//获取应用程序
const app = getApp();

Page({
  data:{
    imgUrls:[
      '../../assets/img/img1.jpg',
      '../../assets/img/img2.jpg',
      '../../assets/img/img3.jpg',
      '../../assets/img/img4.jpg',
    ],
    indicatorDots:true,
    autoplay:true,
    interval:2000,
    duration:1000,
    movies:[],
    hiddenLoading:true,
    winHeight:0,
    start:0,
    more:false
  },
  onLoad:function()
  {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
          that.setData({
            winHeight: res.windowHeight
          });
      },
    })
    this.getHotMovies();
  },
  getHotMovies:function()
  {
      var that = this;
      that.setData({
        hiddenLoading:!that.data.hiddenLoading
      });

    //用同步的方式去获取缓存当中的经纬度
    var latitude = wx.getStorageSync('latitude');
    var longitude = wx.getStorageSync('longitude');
    var city = wx.getStorageSync('city');
    if (latitude && longitude && city)
    {
      that.getMoviesData(city);
    }else{
      //获取用户地理信息
      wx.getLocation({
        success: function (res) {
          //纬度
          var latitude = res.latitude;
          //经度
          var longitude = res.longitude;
          //输出类型
          var output = "json";
          //key 
          var key = "ubnAS9NZvICXBhNbwMF9S5P6eM8hCcIe";

          //当获取到经纬度之后，把经纬度保存到缓存当中去
          wx.setStorageSync('latitude', res.latitude);
          wx.setStorageSync('longitude', res.longitude);

          var url = `http://api.map.baidu.com/geocoder/v2/?location=${latitude},${longitude}&output=${output}&ak=${key}`;

          //根据经纬度获取城市名称
          wx.request({
            url: url,
            data: {},
            header: { "Content-Type": "json" },
            success: function (res) {
              var city = res.data.result.addressComponent.city;
              wx.setStorageSync('city', city);
              if (city) {
                that.getMoviesData(city);
              }
            }
          })
        },
      })
    }



      
  },
  getMoviesData:function(city)
  {
    var that = this;
    var start = that.data.start;
    console.log(start);
    //请求数据部分
    wx.request({
      //url: `https://api.douban.com/v2/movie/in_theaters?city=${city}`,
      url: `https://douban.uieee.com/v2/movie/in_theaters?city=${city}&start=${start}`,
      data: {},
      header: { "Content-Type": "json" },
      success: function (res) {
        if (JSON.stringify(res.data.subjects) != "[]")
        {
          that.setData({
            start: start + 10,
            hiddenLoading: !that.data.hiddenLoading,
            movies: that.data.movies.concat(res.data.subjects)
          });
        }else{
          that.setData({
            hiddenLoading: !that.data.hiddenLoading,
            more:true
          });
        }
          
      }
    })
  },
  update:function(){
      //更新数据
      var that = this;
      that.setData({
        more:false,
        start:0,
        hiddenLoading:false,
        movies:[]
      });

      var city = wx.getStorageSync('city');
      that.getMoviesData(city);
  },
  appendData:function()
  {
    var that = this;
    if(!that.data.more)   //判断当还可以获取数据的时候在进行调用
    {
      that.setData({
        hiddenLoading: false,
      });
      var city = wx.getStorageSync('city');

      that.getMoviesData(city);
    }
    
  },
  //分享
  onShareAppMessage:function(res)
  {
    return {
      title: '电影APP',
      path: 'pages/movies/movies'
    }
  }
});